#!/usr/bin/env python
# coding=utf-8
#
# Copyright 2015 pythonweb.me
# Do have a faith in what you're doing.
# Make your life a story worth telling.

# cat /etc/mime.types
# application/octet-stream    crx

# reload(sys)
# sys.setdefaultencoding("utf8")
import sys
sys.setrecursionlimit(10000)

import os.path

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options

from lib import torndb
from lib.loader import Loader
from lib.session import Session, SessionManager
from jinja2 import Environment, FileSystemLoader

define("port", default = 8080, help = "run on the given port", type = int)
define("mysql_host", default = "localhost", help = "community database host")
define("mysql_database", default = "pythonweb", help = "community database name")
define("mysql_user", default = "root", help = "community database user")
define("mysql_password", default = "123", help = "community database password")

# 这里是导入MongoDB

from route import *

# from pycket.session import SessionManager


class Application(tornado.web.Application):
    def __init__(self):
        handlers = HANDLERS

        settings = dict(
            blog_title = u"F2E Community",
            template_path = os.path.join(os.path.dirname(__file__), "templates"),
            static_path = os.path.join(os.path.dirname(__file__), "static"),
            xsrf_cookies = True,
            cookie_secret = "cookie_secret_code",
            login_url = "/login",
            autoescape = None,
            jinja2 = Environment(loader = FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates")), trim_blocks = True),
            reserved = ["user", "register", "admin"],
            debug = True,
        )

        tornado.web.Application.__init__(self, handlers, **settings)

        # Have one global connection to the blog DB across all handlers
        self.db = torndb.Connection(
            host = options.mysql_host, database = options.mysql_database,
            user = options.mysql_user, password = options.mysql_password
        )

        # self.db = pymongo.MongoClient('localhost',27017).pythonweb

        # Have one global loader for loading models and handles
        self.loader = Loader(self.db)

        # Have one global model for db query
        self.user_model = self.loader.use("user.model")
        self.topics_model = self.loader.use("topics.model")

        # Have one global session controller
        self.session_manager = SessionManager(settings["cookie_secret"], ["127.0.0.1:11211"], 300)

        # Have one global memcache controller
        # self.mc = memcache.Client(["127.0.0.1:11211"])

        # Have one global redis controller
        # self.redis = redis.Redis(host='localhost', port=6379, db=0)


def main():
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
    tornado.autoreload.start()

if __name__ == "__main__":
    main()